<template>
    <div>

        <div class="title">
            <div class="desc">工作经历</div>
        </div>

        <el-table :data="membersList" class="table">
            <el-table-column label="开始时间">
                <template #default="scope">
                    <el-date-picker v-if="activeIndex == scope.$index" v-model="scope.row.startTime" type="data" @change="changeStartTime(scope.row.startTime,scope.row.endTime,scope.$index)"
                        value-format="YYYY-MM-DD" placeholder="开始时间" clearable style="width: 100%" />
                    <span v-else>{{ scope.row.startTime }}</span>
                </template>
            </el-table-column>

            <el-table-column label="结束时间">
                <template #default="scope">
                    <el-date-picker v-if="activeIndex == scope.$index" v-model="scope.row.endTime" type="data" @change="changeEndTime(scope.row.startTime,scope.row.endTime,scope.$index)"
                        value-format="YYYY-MM-DD" placeholder="结束时间" clearable style="width: 100%" />
                    <span v-else>{{ scope.row.endTime }}</span>
                </template>
            </el-table-column>

            <el-table-column label="所在单位">
                <template #default="scope">
                    <el-input v-if="activeIndex == scope.$index" v-model="scope.row.workUnitId"
                        placeholder="请输入所在单位"></el-input>
                    <span v-else>{{ scope.row.workUnitId }}</span>
                </template>
            </el-table-column>
            <el-table-column label="时长(月)" >
                <template #default="scope">
                    <span >{{ scope.row.duration }}</span>
                </template>
            </el-table-column>
            <el-table-column label="岗位名称">
                <template #default="scope">
                    <el-input v-if="activeIndex == scope.$index" v-model="scope.row.positionName"
                        placeholder="请输入岗位名称"></el-input>
                    <span v-else>{{ scope.row.positionName }}</span>
                </template>
            </el-table-column>
            <el-table-column label="所在国家">
                <template #default="scope">

                    <el-select placeholder="所在国家" v-if="activeIndex == scope.$index" v-model="scope.row.countryId"
                        @change="changeCountry($event, activeIndex)" style="width: 125px; margin-right: 15px" clearable>
                        <el-option :label="item.itemText" :value="item.itemValue" :key="item.itemValue"
                            v-for="item in countryList" />
                    </el-select>

                    <span v-else>{{ scope.row.country }}</span>
                </template>
            </el-table-column>
            <el-table-column align="right" width="250">
                <template #header>
                    <span>操作</span>
                </template>
                <template #default="scope">
                    <div v-if="activeIndex == scope.$index">

                        <el-button type="success" @click="handleSave">保存</el-button>
                    </div>
                    <div v-else>
                        <el-button type="primary" @click="handleEdit(scope.$index)">编辑</el-button>
                        <el-popconfirm @confirm="handleDelete(scope.$index)" width="220" confirm-button-text="确定"
                            cancel-button-text="取消" icon-color="#e6a23c" title="你确定要删除当前行?">
                            <template #reference>
                                <el-button type="danger">删除</el-button>
                            </template>
                        </el-popconfirm>
                    </div>
                </template>
            </el-table-column>
        </el-table>
    </div>
</template>



<script  lang="ts">

import { tips } from "@/views/science/components/tips/tips";
import { ref, watch } from 'vue';

export default {


    components: {

    },
    emits: {
        dataChange: null,
        paramsChange: null,
    },
    props: {
        members: {
            type: Array,
            required: true,
        },
        countryList: {
            type: Array,
            required: true,

        }

    },
    setup(props, { emit }) {

        let activeIndex = ref(-1);


        const membersList = ref(props.members) as any

        watch(props, (newValue, oldValue) => {
            membersList.value = newValue.members
        });


        const handleAdd = () => {

            let item = {
                startTime: '',
                endTime: '',
                workUnitId: '',
                workUnitName: '',
                positionName: '',
                duration: 0,
                id: '',
                country: '',
                countryId: '',
            };

            membersList.value.push(item);

            activeIndex.value = membersList.value.length - 1;

        }



        const handleEdit = (index: number) => {

            activeIndex.value = index;
        };

        const handleSave = () => {

            activeIndex.value = -1;

            emit('paramsChange', membersList.value)

        };

        let changeCountry = (id: any, currentRow: any) => {
            let itemText = props.countryList.find((item: any) => item.itemValue == id).itemText;
            membersList.value[currentRow].countryId = id
            membersList.value[currentRow].country = itemText
        }

        const handleDelete = (index: number) => {
            membersList.value.splice(index, 1);
            emit('paramsChange', membersList.value)
        };

        const changeStartTime = (startTime,endTime,index) =>{
            let start = new Date(startTime).getTime()
            let end = new Date(endTime).getTime()
            if(start > end){
              tips("开始时间需要小于结束时间", "warning");
              membersList.value[index].startTime = ''
            }
            let duration = Math.trunc((end - start) / 2592000000)
            membersList.value[index].duration = duration > 0 ? duration : 0
            // console.log('changeStartTime',duration,membersList.value,index,);
        }
        const changeEndTime = (startTime,endTime,index) =>{
            let start = new Date(startTime).getTime()
            let end = new Date(endTime).getTime()
            if(start > end){
                tips("开始时间需要小于结束时间", "warning");
                membersList.value[index].endTime = ''
            }
            let duration = Math.trunc((end - start) / 2592000000)
            membersList.value[index].duration = duration > 0 ? duration : 0
        }

        return {
            membersList,
            activeIndex,
            handleAdd,
            handleEdit,
            handleSave,
            handleDelete,
            changeCountry,
            changeStartTime,
            changeEndTime
        }


    },
}
</script>












<style scoped lang="scss">
.title {
    display: flex;
    justify-content: space-between;

    .desc {
        font-weight: bolder;
    }
}

.table {
    margin: 15px 0;
}
</style>
